package com.original.simuladores.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;

import com.original.simuladores.dao.GenericDao;

public class GenericDaoImpl<T> implements GenericDao<T> {
	@Autowired
	SessionFactory sessionFactory;
	
	/*
	 * @see com.original.simuladores.dao.GenericDao#create(java.lang.Object)
	 */
	@Override
	public Serializable create(T instance) {
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		session.saveOrUpdate(instance);
		tx.commit();
		Serializable id = session.getIdentifier(instance);
		session.close();
		return (Serializable) id;

	}

	/*
	 * @see com.original.simuladores.dao.GenericDao#update(java.lang.Object)
	 */
	@Override
	public Serializable update(T instance) {
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		session.update(instance);
		tx.commit();
		Serializable id = session.getIdentifier(instance);
		session.close();
		return (Serializable) id;
	}

	/*
	 * @see com.original.simuladores.dao.GenericDao#delete(java.lang.Class, java.io.Serializable)
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Serializable delete(Class instance, Serializable id) {
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		T t = (T) session.load(instance, id);
		session.delete(t);
		tx.commit();
		Serializable ids = session.getIdentifier(t);
		session.close();
		return (Serializable) ids;
	}

	/*
	 * @see com.original.simuladores.dao.GenericDao#read(java.lang.Class, java.lang.Integer)
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<T> read(Class instance, Serializable id) {
		Session session = sessionFactory.openSession();
		List<T> lsT = session.createQuery("from " + getNameClass(instance.toString()) + " WHERE id = " + id).list();
		session.close();
		return lsT;
	}
	
/*	
	 * @see com.original.simuladores.dao.GenericDao#read(java.lang.Class, java.lang.String)
	 
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<T> read(Class instance, String id) {
		Session session = sessionFactory.openSession();
		List<T> lsT = session.createQuery("from " + getNameClass(instance.toString()) + " WHERE id = " + id).list();
		session.close();
		return lsT;
	}*/
	
	/*
	 * @see com.original.simuladores.dao.GenericDao#read(java.lang.Class)
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<T> read(Class instance) {
		Session session = sessionFactory.openSession();
		List<T> lsT = session.createQuery("from " + getNameClass(instance.toString())).list();
		session.close();
		return lsT;
	}
	
	/*
	 * @see com.original.simuladores.dao.GenericDao#lastestObject(java.lang.Class)
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<T> lastestObject(Class instance) {
		Session session = sessionFactory.openSession();
		List<T> lsT = session.createQuery("from " + getNameClass(instance.toString()) + " A ORDER BY A.id DESC").list();
		session.close();
		return lsT;
	}

	/**
	 * get Name of Class
	 * 
	 * @param strInput
	 * @return Class name
	 */
	private String getNameClass(String strInput) {
		String[] spt = strInput.split("\\.");
		return spt[spt.length - 1];
	}
}
